/*-----------------------------------------------------------------------------*/
/* ExternAdmin.java                                                              */
/* external administrator for ecom application                                 */
/* Fabienne Boyer - Didier Donsez may 2006                                     */
/*-----------------------------------------------------------------------------*/

package ecom.client;

import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Pattern;

import ecom.beans.*;
import ecom.pojos.*;
import javax.transaction.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

// RMI administrator for the ecom application.

public class ExternAdmin {

	int accountId = 0;
	Context initialContext = null;
	UserTransaction utx = null;

	public static void main(String[] args) {
		try {

			System.out.println("Begining Client...");

			// if user don't use jclient/client container
			System
					.setProperty(Context.INITIAL_CONTEXT_FACTORY,
							"org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");

			// parseArgs(args);

			// get JNDI initial context
			Context initialContext = null;
			try {
				initialContext = new InitialContext();
			} catch (Exception e) {
				System.err.println("Cannot get initial context for JNDI: " + e);
				System.exit(2);
			}

			// We want to start transactions from client: get UserTransaction
			UserTransaction utx = null;
			try {
				utx = (UserTransaction) initialContext
						.lookup("javax.transaction.UserTransaction");
			} catch (NamingException e) {
				System.err.println("Cannot lookup UserTransaction: " + e);
				System.exit(2);
			}

			EcomAdminRemote ecomAdminBean = null;
			try {
				ecomAdminBean = (EcomAdminRemote) initialContext
						.lookup("ecom.beans.EcomAdminBean" + "_"
								+ EcomAdminRemote.class.getName() + "@Remote");
			} catch (NamingException e) {
				System.err.println("Cannot get EcomAdminBean : " + e);
				System.exit(2);
			}

			/*
			 * <<<<<<< .mine
			 * System.out.println(EcomAdminRemote.class.getName());
			 * 
			 * EcomAdminRemote ecomAdminBean = null; try { ecomAdminBean =
			 * (EcomAdminRemote) initialContext
			 * .lookup("ecom.beans.EcomAdminBean" + "_" +
			 * EcomAdminRemote.class.getName() + "@Remote"); } catch
			 * (NamingException e) { System.err.println("Cannot get
			 * EcomAdminBean : " + e); System.exit(2); } =======
			 */
			ValidateRegEx val = new ValidateRegEx();
			while (true) {
				System.out
						.println("------------------------------------------------------");
				System.out.println("Enter a command :");
				System.out.println("1 = create an account ");
				System.out.println("2 = create a productStore ");
				System.out.println("3 = create a product ");
				System.out.println("4 = create a customer ");
				System.out.println("5 = create a category of products ");
				System.out.println("6 = get account list ");
				System.out.println("7 = get product list ");
				System.out.println("8 = get customer list ");
				System.out.println("9 = View unanswered technical questions");
				System.out.println("10 = Answer a technical question");
				System.out.println("0 = quit the application ");
				/*
				 * >>>>>>> .r22 <<<<<<< .mine while (true) {
				 * System.out.println("------------------------------------------------------");
				 * System.out.println("Enter a command :");
				 * System.out.println("1 = create an account ");
				 * System.out.println("2 = create a productStore");
				 * System.out.println("3 = create a product ");
				 * System.out.println("4 = create a customer ");
				 * System.out.println("5 = get account list");
				 * System.out.println("6 = get product list");
				 * System.out.println("7 = create a customer ");
				 * System.out.println("8 = create a category of products ");
				 * System.out.println("9 = View unanswered technical
				 * questions"); System.out.println("10 = Answer a technical
				 * question"); System.out.println("0 = quit the application ");
				 * =======
				 */
				int choice = Tx.readInt();

				if (choice == 1) {
					/*
					 * System.out.println("Please enter account ID : "); int id =
					 * Tx.readInt();
					 */

					/*
					 * System.out.println("Please enter your email : "); int
					 * ownerId = Tx.readInt();
					 */
					System.out
							.println("Please enter your email (xxx@xxx.xxx format) : ");
					String emailId = "";
					//boolean authEmail = false;
					//while (!authEmail) {
						emailId = Tx.readString();
						/*if (val.valideEmail(emailId)) {
							authEmail = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}*/
						
					//}

					System.out.println("Please enter account balance : ");
					Double bal = Tx.readDouble();
					ecomAdminBean.createAccount(emailId, bal);

					System.out.println(("Account creation success : ") + ecomAdminBean.getAccountName(emailId));

				} else if (choice == 2) {
					System.out.println("-- not yet implemented\n");
				} else if (choice == 3) {
					System.out.println("Please enter the product reference : ");
					String ref = Tx.readString();

					System.out
							.println("Please enter a category ID for this product : ");
					String cat = Tx.readString();

					System.out.println("Please enter the product name : ");
					String name = Tx.readString();

					System.out.println("Please enter the product brand : ");
					String brand = Tx.readString();

					System.out
							.println("Please enter the product description : ");
					String descr = Tx.readString();

					System.out.println("Please enter the product price : ");
					double price = Tx.readDouble();

					System.out
							.println("Please enter the amoutn of initial stock : ");
					double stock = Tx.readDouble();

					ecomAdminBean.createProduct(ref, name, brand, price, cat,
							descr, stock);

					System.out.println("Product creation success c: "
							+ ecomAdminBean.getProductName(ref));
				} else if (choice == 0) {
					System.out.println("Goodbye.\n");
					System.exit(0);
				} else if (choice == 4) {
	
					System.out.println("Please enter a password : ");
					String pwdString = Tx.readString();
					AuthBean authBean = new AuthBean();
					byte[] pwdCust = authBean.setPwdtoMD5(pwdString);

					System.out.println("Please enter the name : ");
					String nomCust = "";
					boolean authNom = false;
					while (!authNom) {
						nomCust = Tx.readString();
						if (val.valideNom(nomCust)) {
							authNom = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}
					}

					System.out.println("Please enter the surname  : ");
					String prenomCust = "";
					boolean authPrenom = false;
					while (!authPrenom) {
						prenomCust = Tx.readString();
						if (val.validePrenom(prenomCust)) {
							authPrenom = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}
					}

					System.out
							.println("Please enter the exact home address (Street number then street name) : ");
					String addrCust = "";
					boolean authAddr = false;
					while (!authAddr) {
						addrCust = Tx.readString();
						if (val.valideAddress(addrCust)) {
							authAddr = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}
					}

					System.out
							.println("Please enter the zip code (5 digitals) : ");
					int zipCust = 0;
					boolean authZip = false;
					while (!authZip) {
						zipCust = Tx.readInt();
						String t = String.valueOf(zipCust);
						if (val.valideCode(t)) {
							authZip = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}
					}

					System.out
							.println("Please enter the country of residence : ");
					String paysCust = "";
					boolean authPays = false;
					while (!authPays) {
						paysCust = Tx.readString();
						if (val.validePays(paysCust)) {
							authPays = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}
					}

					System.out
							.println("Please enter the phone number (00 00 00 00 00): ");
					String telCust = "";
					boolean authTel = false;
					while (!authTel) {
						telCust = Tx.readString();
						if (val.valideTel(telCust)) {
							authTel = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}
					}

					System.out
							.println("Please enter your email (xxx@xxx.xxx format) : ");
					String emailCust = "";
					boolean auth = false;
					while (!auth) {
						emailCust = Tx.readString();
						if (val.valideEmail(emailCust)) {
							auth = true;
						} else {
							System.out
									.println("Sorry, wrong syntax, please try again :");
						}
					}

					ecomAdminBean.createCustomer(pwdCust, nomCust,
							prenomCust, addrCust, zipCust, paysCust, telCust,
							emailCust);

					// System.out.println("Customer cree : "+
					// ecomAdminBean.getCustomer(idCust));
					System.out.println("\nCustomer creation success.\n");

				} else if (choice == 9) {
					Vector<TechQuestion> res = ecomAdminBean
							.getUnansweredTechQuestion();
					Iterator<TechQuestion> ite = res.iterator();
					while (ite.hasNext()) {
						TechQuestion cur = ite.next();
						System.out.println("id = " + cur.getId()
								+ ", product : " + cur.getRef()
								+ ", question : " + cur.getQuestion());
					}
				} else if (choice == 10) {
					System.out.println("Please enter the question ID : ");
					int idQ = Tx.readInt();
					System.out.println("Enter the answer : ");
					String answer = Tx.readString();
					ecomAdminBean.answerTechQuestion(idQ, answer);

				}
				/*
				 * else { System.out.println("Bad choice.\n"); }}} catch
				 * (Exception e) { System.err.println("Client get an exception : " +
				 * e.getLocalizedMessage()); System.exit(2); } =======
				 */
				else if (choice == 5) {
					System.out.println("Please enter the category ID : ");
					int idCateg = Tx.readInt();
					// >>>>>>> .r22

					System.out.println("Please enter the category name : ");
					String name = Tx.readString();

					ecomAdminBean.createCategory(idCateg, name);

					// System.out.println("Customer cree : "+
					// ecomAdminBean.getCustomer(idCust));
					System.out.println("Category creation success.\n");

				} else if (choice == 6) {
					System.out.println(ecomAdminBean.getAllAccountsName()+"\n");
				} else if (choice == 7) {
					System.out.println(ecomAdminBean.getAllProductsName()+"\n");
				} else if (choice == 8) {
					System.out.println(ecomAdminBean.getAllCustomersName()+"\n");
				} else {
					System.out.println("Bad choice.\n");
				}
			}
		} catch (Exception e) {
			System.err.println("Client get an exception : "
					+ e.getLocalizedMessage());
			System.exit(2);
		}

	}

	/**
	 * Parses an argument list for an acountId
	 * 
	 * @param args[]
	 *            String array of arguments
	 */

	static void parseArgs(String args[]) {
		System.out.println("ParseArgs not yet implemented");
	}

}
